package day9;

import java.util.Scanner;

/**
 * 字节笔试
 */
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main3 {
    public static void main1(String[] args) {
        Scanner in = new Scanner(System.in);
        // 注意 hasNext 和 hasNextLine 的区别
        while (in.hasNextInt()) { // 注意 while 处理多个 case
            int n = in.nextInt();
            int q = in.nextInt();
            int[] a = new int[n];
            int[] b = new int[n];
            for(int i = 0; i < n; i++) {
                a[i] = in.nextInt();
            }
            for(int i = 0; i < n; i++) {
                b[i] = in.nextInt();
            }
            int[][] arr = new int[q][3];
            for(int i = 0; i < q; i++) {
                for(int j = 0; j < 3; j++) {
                    arr[i][j] = in.nextInt();
                }
            }
            for(int i = 0; i < q; i++) {
                int sum = 0;
                //数组a交换
                if(arr[i][0] == 1) {
                    swap(a,arr[i][1]-1, arr[i][2]-1);
                }else {
                    swap(b,arr[i][1]-1, arr[i][2]-1);
                }
                //交换完找出ab对应的最大值相加
                for(int j = 0; j < n; j++) {
                    int max = a[j] > b[j] ? a[j] : b[j];
                    sum += max;
                }
                System.out.println(sum);
            }
        }
    }
    private static void swap1(int[] arr, int x, int y) {
        int tmp = arr[x];
        arr[x] = arr[y];
        arr[y] = tmp;
    }
    public static void main3(String[] args) {
        Scanner in = new Scanner(System.in);
        // 注意 hasNext 和 hasNextLine 的区别
        int n = in.nextInt();
        int q = in.nextInt();
        int[] a = new int[n];
        int[] b = new int[n];
        for (int i = 0; i < n; i++) {
            a[i] = in.nextInt();
        }
        for (int i = 0; i < n; i++) {
            b[i] = in.nextInt();
        }
        int[] c = new int[n];
        for(int i = 0; i < n; i++) {
            c[i] = a[i] > b[i] ? a[i] : b[i];
        }
        int sum = 0;
        for(int cur : c) {
            sum += cur;
        }
        while(q > 0) {
            int op = in.nextInt();
            int x = in.nextInt();
            int y = in.nextInt();
            if(op == 1) {
                sum = sum - c[x-1] - c[y-1];
                swap(a, x-1, y-1);
                int max1 = a[x-1] > b[x-1] ? a[x-1] : b[x-1];
                int max2 = a[y-1] > b[y-1] ? a[y-1] : b[y-1];
                c[x-1] = max1;
                c[y-1] = max2;
                sum += max1;
                sum += max2;
            } else {
                sum = sum - c[x-1] - c[y-1];
                swap(b, x-1, y-1);
                int max1 = a[x-1] > b[x-1] ? a[x-1] : b[x-1];
                int max2 = a[y-1] > b[y-1] ? a[y-1] : b[y-1];
                c[x-1] = max1;
                c[y-1] = max2;
                sum += max1;
                sum += max2;
            }
            q--;
            System.out.println(sum);
        }
    }




    public static void main2(String[] args) {
        Scanner in = new Scanner(System.in);
        // 注意 hasNext 和 hasNextLine 的区别
        int n = in.nextInt();
        int q = in.nextInt();
        in.nextLine();

        String str = in.nextLine();

        while (q > 0) { // 注意 while 处理多个 case
            String search = in.nextLine();
            // System.out.println(n);
            // System.out.println(q);
            // System.out.println(str);
            // System.out.println(search);
            int count = 0;
            for (int i = 0; i < n; i++) {
                int j = 0;
                int cur = i;
                while (j < search.length() && cur < n && str.charAt(cur) == search.charAt(j)) {
                    j++;
                    cur++;
                }
                if (j == search.length()) {
                    count++;
                } else {
                    continue;
                }
            }
            System.out.println(count);
            q--;

        }
    }
    public static void main4(String[] args) {
        Scanner in = new Scanner(System.in);
        // 注意 hasNext 和 hasNextLine 的区别
        int n = in.nextInt();
        int q = in.nextInt();
        in.nextLine();

        String str = in.nextLine();

        while (q > 0) { // 注意 while 处理多个 case
            String search = in.nextLine();
            int count = 0;
            for (int i = 0; i < n; i++) {
                if (str.charAt(i) == search.charAt(0) && i + search.length() < n) {
                    String cur = str.substring(i, i + search.length());
                    if (cur.equals(search)) {
                        count++;
                    }
                }
            }
            System.out.println(count);
            q--;
        }
    }
        public static void main(String[] args) {
            Scanner sc = new Scanner(System.in);
            while (sc.hasNext()) {
                int n = sc.nextInt();
                int q = sc.nextInt();
                int[] a = new int[n];
                int[] b = new int[n];
                int sum = 0;
                for (int i = 0; i < n; i++) {
                    a[i] = sc.nextInt();
                }
                for (int i = 0; i < n; i++) {
                    b[i] = sc.nextInt();
                }


                for (int i = 0; i < n; i++) {
                    sum += Math.max(a[i], b[i]);
                }


                for (int i = 0; i < q; i++) {
                    int op = sc.nextInt();
                    if (op == 1) {
                        int left = sc.nextInt()-1;
                        int right = sc.nextInt()-1;
                        sum -= (Math.max(a[left], b[left]) + Math.max(a[right], b[right]));
                        swap(a, left, right);
                        sum+=(Math.max(a[left], b[left]) + Math.max(a[right], b[right]));
                        System.out.println(sum);
                    } else {
                        int left = sc.nextInt()-1;
                        int right = sc.nextInt()-1;
                        sum -= (Math.max(a[left], b[left]) + Math.max(a[right], b[right]));
                        swap(b, left, right);
                        sum+=(Math.max(a[left], b[left]) + Math.max(a[right], b[right]));
                        System.out.println(sum);
                    }
                }
            }
        }
        public static void swap(int[] arr, int i, int j) {
            int temp = arr[i];
            arr[i] = arr[j];
            arr[j] = temp;
        }
    }

